package com.example.asus.picselector.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.DisplayMetrics;

import com.example.asus.picselector.constants.Constants;

public class Util {

    private static AlertDialog.Builder builder;
    private static boolean isShow;

    @SuppressLint("NewApi")
    public static void jumpPerssiomSetting(final Activity activity, String msg) {
        if (!isShow) {
            builder = new AlertDialog.Builder(activity);
            builder.setMessage(msg)
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which) {
                            // TODO Auto-generated method stub
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                            intent.setData(uri);
                            activity.startActivityForResult(intent, Constants.SETTING_PERSSIOM);
                            isShow = false;
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    builder.create().dismiss();
                    isShow = false;
                }
            });
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    isShow = false;
                }
            });
            builder.create().show();
            isShow = true;
        }
    }

    /**
     * 根据手机的屏幕属性从 dip 的单�?转成�?px(像素)
     *
     * @param context
     * @param value
     * @return
     */
    public static int dip2px(Context context, float value) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (value * metrics.density + 0.5f);
    }
}
